import Duck from 'extensible-duck'
import produce from 'immer'

export default new Duck({
  namespace: 'admin',
  store: 'modal',
  types: ['OPEN', 'CLOSE', 'CANCLE', 'INIT'],
  initialState: {
    isOpen: false,
    config: {},
  },
  reducer: produce((draft, action, duck) => {
    switch (action.type) {
      case duck.types.OPEN:
        draft.isOpen = true
        break
      case duck.types.CLOSE:
        draft.isOpen = false
        break
      case duck.types.INIT:
        draft.config = action.config
        draft.isOpen = true
        break
      default:
        break
    }
  }),
  selectors: {
    isOpen: state => state.modal.isOpen,
    modal: state => state.modal,
  },
  creators: duck => ({
    actionOpen: () => ({ type: duck.types.OPEN }),
    actionClose: func => ({ type: duck.types.CLOSE, func }),
    actionInit: config => ({ type: duck.types.INIT, config }),
  }),
})
